package com.robcity.platform.application;

import com.robcity.platform.dto.RobotEventInfoSaveDTO;
import com.robcity.platform.dto.RobotLocationInfoSaveDTO;
import com.robcity.platform.entities.RobotEventInfo;
import com.robcity.platform.param.RobotReqCommon;
import com.robcity.platform.repo.RobotEventInfoRepo;
import com.robcity.platform.util.GeoFenceChecker;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description
 * @Author zhangsong
 * @Date 2024/8/10
 **/
@Component
@Slf4j
public class RobotEventInfoApplication {
    @Resource
    RobotEventInfoRepo robotEventInfoRepo;
    @Resource
    UdpClientServiceApplication udpClientServiceApplication;
    //代表未进入巡视取
    private static String INAREA = "0";
    //代表无人机是否到达事件地点
    private static String UAV_INAREA_STATUS = "0";
    @Value("${llm.ip}")
    private String llmIp;
    @Value("${llm.port}")
    private Integer llmPort;
    public void RobotEventInfoCreateHandle(RobotReqCommon common, RobotLocationInfoSaveDTO robotLocationInfoDTO) {
       //判断是否已经存在事件
       List< RobotEventInfo> robotEventInfos =  robotEventInfoRepo.selectRobotEventInfo(robotLocationInfoDTO);
        if(!CollectionUtils.isEmpty(robotEventInfos)){
            return;
        }
        RobotEventInfoSaveDTO robotEventInfoDTO = new RobotEventInfoSaveDTO();
        //默认为人群聚集
        robotEventInfoDTO.setEventType("1");
        // 处理状态初始化为持续中
        robotEventInfoDTO.setHandelStatus("1");
        robotEventInfoDTO.setOccurLat(robotLocationInfoDTO.getLat());
        robotEventInfoDTO.setOccurLon(robotLocationInfoDTO.getLon());
        robotEventInfoDTO.setRobId(robotLocationInfoDTO.getRob_id());
        robotEventInfoDTO.setTimeStemp(System.currentTimeMillis() + "");
        robotEventInfoDTO.setTaskId(robotLocationInfoDTO.getTaskId());
        robotEventInfoDTO.setTaskType(robotLocationInfoDTO.getTaskType());
        robotEventInfoRepo.insert(robotEventInfoDTO);
        //发生人群聚集发送消息给大漠型
        log.info("-----------------发送消息给大漠型:发生人群聚集----------------");
        udpClientServiceApplication.sendUdpMessage("发生人群聚集", llmIp, llmPort);
    }

    public void RobotEventInfoCloseHandle(RobotReqCommon common, RobotLocationInfoSaveDTO robotLocationInfoDTO) {
        robotEventInfoRepo.RobotEventInfoClose(robotLocationInfoDTO);
        //人群聚集已散开发送消息给大漠型
        log.info("-----------------发送消息给大漠型:人群聚集已散开----------------");
        udpClientServiceApplication.sendUdpMessage("人群聚集已散开", llmIp, llmPort);

    }
    @Synchronized
    public void RobotCheckAreaHandle(RobotReqCommon common, RobotLocationInfoSaveDTO robotLocationInfoDTO) {
        //判断是否在区域内
        Boolean isInArea = GeoFenceChecker.isPointInRadius(114.2136099,22.6954046,robotLocationInfoDTO.getLon().doubleValue(),robotLocationInfoDTO.getLat().doubleValue(),10);
        if(INAREA.equals("0")&&isInArea){
            INAREA="1";
            //改变状态 并发送巡检开始
            log.info("-----------------发送消息给大漠型:重点设施巡逻开始----------------");
            udpClientServiceApplication.sendUdpMessage("重点设施巡逻开始", llmIp, llmPort);
        }
        if(INAREA.equals("1")&&!isInArea){
            //改变状态 并发送巡检结束
            INAREA="0";
            log.info("-----------------发送消息给大漠型:重点设施检查完成----------------");
            udpClientServiceApplication.sendUdpMessage("重点设施检查完成", llmIp, llmPort);

        }
    }
    @Synchronized
    public void uavCheckAreaHandle(RobotReqCommon common, RobotLocationInfoSaveDTO robotLocationInfoDTO) {
        //判断是否在区域内
        Boolean isInArea = GeoFenceChecker.isPointInRadius(114.2136099,22.6954046,robotLocationInfoDTO.getLon().doubleValue(),robotLocationInfoDTO.getLat().doubleValue(),10);
        if(UAV_INAREA_STATUS.equals("0")&&isInArea){
            UAV_INAREA_STATUS="1";
            //无人机已经到达事件地点
            log.info("-----------------发送消息给大漠型:无人机已经到达事件地点----------------");
            udpClientServiceApplication.sendUdpMessage("无人机已经到达事件地点", llmIp, llmPort);
        }
    }
}
